﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2015-2019 Jean-Marc VIGLINO, 
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: Flow style</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  
  <meta name="description" content="Draw flow maps or Sankey maps with Openlayers." />
  <meta name="keywords" content="ol, openlayers, vector, style, stroke, width, color, variable, sankey, flow" />

  <link rel="stylesheet" href="../style.css" />

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
  <!-- FontAwesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://openlayers.org/en/latest/css/ol.css" />
  <script type="text/javascript" src="https://openlayers.org/en/latest/build/ol.js"></script>
  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL,Object.assign"></script>
  
  <!-- ol-ext -->
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>

  <style>
    .options label {
      display: inline-block;
      text-align: right;
      min-width: 4em;
    }
    .options input[type="number"] {
        width: 5em;
    }
  </style>
</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: Flow line style</h1>
  </a>
  <div class="info">
    <p>
      The <i>ol.style.FlowLine</i> is a line style to draw LineString with variable colors and widths.
      <br/>
      This can be used to display <a href="https://en.wikipedia.org/wiki/Flow_map">flows</a> maps
      or <a href="https://en.wikipedia.org/wiki/Sankey_diagram">Sankey diagram</a> on a map.
    </p>
    <ul>
      <li>
        The <i>widh</i> and <i>width2</i> options can be used to pass the initial and the final width of the lines.
      </li>
      <li>
        The <i>widh</i> options can be a function that returns the current width giving a feature and a step (beetween [0,1]).
      </li>
      <li>
        The <i>color</i> and <i>color2</i> options can be used to pass the initial and the final color of the lines.
      </li>
      <li>
        The <i>color</i> options can be a function that returns the current color giving a feature and a step (beetween [0,1]).
      </li>
    </ul>
    <p>
      NB: the FlowLine style doesn't impress the hit-detection.
      If you want your lines to be sectionable you have to add a style (with transparent line: 
      stroke color opacity to .1 or zero width) to handle this 
      (see defaultStyle in example or this <a href="https://github.com/Viglino/ol-ext/issues/420">issue</a>).
    </p>
    <p>
      Look at <a href='map.style.flowline2.html'>this example</a> to see how to set a width and color function to control the line drawing.
      <br/>
      It can be used to display elevation as color on a <a href='map.style.gpxline.html'>GPX tracks</a>.
    </p>
  </div>

  <!-- Map div -->
  <div id="map" style="width:600px; height:400px;"></div>

  <div class="options" style="min-width:300px;">
    <h2>Options:</h2>
    <ul>
			<li>
				<label>
					<input type="radio" name="op" value="select" /> 
					Select
				</label>
				<label>
					<input type="radio" name="op" checked="checked" value="drawLine" /> 
					Draw
				</label>
				<label>
					<input type="radio" name="op" value="modify" /> 
					Modify
				</label>
			</li>
			<li>
				<hr/>
        <label>color:</label>
        <select class="color" onchange="flowStyle.setColor(this.value); vector.changed();">
          <option value='red' selected="selected">red</option>
          <option value='yellow'>yellow</option>
          <option value='#0f0'>green</option>
          <option value='#00f'>blue</option>
          <option value='#fff'>white</option>
          <option value='#000'>black</option>
          <option value='rgba(255,255,255,0)'>transparent</option>
        </select>
			</li>
			<li>
        <label>color2:</label>
        <select class="color2" onchange="flowStyle.setColor2(this.value); vector.changed();">
          <option value='red'>red</option>
          <option value='yellow' selected="selected">yellow</option>
          <option value='#0f0'>green</option>
          <option value='#00f'>blue</option>
          <option value='#fff'>white</option>
          <option value='#000'>black</option>
          <option value='rgba(255,255,255,0)'>transparent</option>
        </select>
      </li>
      <li>
        <label>width:</label>
        <input class="width" type="number" value="2" onchange="flowStyle.setWidth(Number(this.value)); vector.changed();" />
      </li>
      <li>
        <label>width2:</label>
        <input class="width2" type="number" value="25" onchange="flowStyle.setWidth2(Number(this.value)); vector.changed();" />
      </li>
      <li>
        LineCap:
        <label><input type="radio" name="cap" value="round" onchange="flowStyle.setLineCap($(this).val()); vector.changed();">round</label>
        <label><input type="radio" name="cap" value="butt" onchange="flowStyle.setLineCap($(this).val()); vector.changed();" checked="checked">butt</label>
      </li>
		</ul>
  </div>
  
<script type="text/javascript">
  
  // Layers
  var layer = new ol.layer.Tile({
    title:'terrain-background',
    source: new ol.source.Stamen({ layer: 'terrain' })
  });

  // The map
  var map = new ol.Map({
    target: 'map',
    view: new ol.View ({
      zoom: 5,
      center: [166326, 5992663]
    }),
    layers: [layer]
  });

  // Default style to make the feature selectable
  var defaultStyle = new ol.style.Style({
    stroke: new ol.style.Stroke({ color: [255, 255, 255, .1], width: 1 })
  });
  // Flow style
  var done = false;
  var flowStyle = new ol.style.FlowLine({
    color: $('.color').val(),
    color2: $('.color2').val(),
    width: Number($('.width').val()),
    width2: Number($('.width2').val())
  });
  function getStyle(feature, res) {
    return [ defaultStyle, flowStyle ];
  }

  // Nouvelle source de donnee
  var vector = new ol.layer.VectorImage({
    source: new ol.source.Vector({ features: new ol.Collection() }),
    style: getStyle
  })
  map.addLayer(vector);
  vector.getSource().addFeature(new ol.Feature(new ol.geom.LineString([[259274, 6398696], [63595, 5958419], [635956, 5772524]])));

  // global so we can remove it later
  var interactions = {
    select: new ol.interaction.Select ({ hitTolerance: 3 }),
    modify: new ol.interaction.Modify ({ features: vector.getSource().getFeaturesCollection() }),
    drawLine: new ol.interaction.Draw({
      type: 'LineString',
      style: getStyle,
      source: vector.getSource()
    })
  }
  for (var i in interactions) map.addInteraction(interactions[i]);

  // Activate interaction
  setInteraction = function () {
    var name = $('[name="op"]:checked').val();
    for (var i in interactions) {
      interactions[i].set("active", (i==name));
    }
  }
  setInteraction();
  $("input:radio").on("change",setInteraction);

</script>

</body>
</html>